/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package bubbleindex;

import com.nativelibs4java.util.IOUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 *
 * @author green
 */
public class noGUI {
    
    enum RunType {
        Single, Category, All, Update
    }
    
    public static String windows;
    public static int threads;
    public static float tCrit;
    public static float m;
    public static float omega;
    public static Boolean forcedCPU;
    
    public static void main(String[] args) {
        
        if (args.length > 0) {
            for (String s: args) {
                System.out.println(s);
            }

            int i = 0;

            if (args[i].equals("noGUI")) {

                BubbleIndex.isGUI = false;
                Indices.initialize();

                try {
                    BuildIndex.src = IOUtils.readText(BuildIndex.class.getResource("TutorialKernels.cl"));
                } catch (IOException ex) {
                    System.out.println("IOException Exception. Code 002.");
                    BubbleIndex.displayOutput("Error. cl Files missing.", false);
                }
                BubbleIndex.displayOutput("Working Dir: " + Indices.userDir, false);

                RunType type = RunType.valueOf(args[++i]);

                if (type == RunType.Single) {
                    BubbleIndex.CategoryName = args[++i];
                    BubbleIndex.SelectionName = args[++i];
                    windows = args[++i];
                    threads = Integer.parseInt(args[++i]);
                    tCrit = Float.parseFloat(args[++i]);
                    m = Float.parseFloat(args[++i]);
                    omega = Float.parseFloat(args[++i]);
                    forcedCPU = Boolean.parseBoolean(args[++i]);
                    
                    System.out.println("Category Name: " + BubbleIndex.CategoryName + " Selection Name: " + BubbleIndex.SelectionName);

                    try {
                        BubbleIndex.RunBubbleIndex();
                    } catch (IOException | ExecutionException | InterruptedException ex) {
                        System.out.println("Exception. Code 003.");
                    }

                }

                else if (type == RunType.Category) {
                    BubbleIndex.CategoryName = args[++i];
                    windows = args[++i];
                    threads = Integer.parseInt(args[++i]);
                    tCrit = Float.parseFloat(args[++i]);
                    m = Float.parseFloat(args[++i]);
                    omega = Float.parseFloat(args[++i]);
                    forcedCPU = Boolean.parseBoolean(args[++i]);

                    System.out.println("Category Name: " + BubbleIndex.CategoryName);

                    ArrayList<String> updateNames = Indices.categoriesAndComponents.get(BubbleIndex.CategoryName).getComponents();

                    for (String updateName : updateNames) {
                        BubbleIndex.SelectionName = updateName;
                        MyThread a = new MyThread();
                        a.start();
                        synchronized (a) {
                            try {
                                a.wait();
                            } catch(InterruptedException e) {
                                System.out.println("Exception. Code 004.");
                                BuildIndex.Stop = true;
                            }
                        }
                    }
                }

                else if (type == RunType.All) {
                    windows = args[++i];
                    threads = Integer.parseInt(args[++i]);
                    tCrit = Float.parseFloat(args[++i]);
                    m = Float.parseFloat(args[++i]);
                    omega = Float.parseFloat(args[++i]);
                    forcedCPU = Boolean.parseBoolean(args[++i]);

                    System.out.println("Running All Categories and Components");

                    for (Map.Entry<String, InputCategory> myEntry : Indices.categoriesAndComponents.entrySet()) {

                        BubbleIndex.CategoryName = myEntry.getKey();
                        ArrayList<String> updateNames = myEntry.getValue().getComponents();

                        for (String updateName : updateNames) {
                            BubbleIndex.SelectionName = updateName;
                            MyThread a = new MyThread();
                            a.start();

                            synchronized (a) {
                                try {
                                    a.wait();
                                } catch(InterruptedException e) {
                                    System.out.println("Interrupted Exception. Code 005.");
                                    BuildIndex.Stop = true;
                                }
                            }
                        }
                    }
                }

                else if (type == RunType.Update) {
                    updateData.updateData();
                }

                else {
                    BubbleIndex.isGUI = true;
                    GUI.GUImain(null);
                }   
            }  
            else {
                BubbleIndex.isGUI = true;
                GUI.GUImain(null);
            }
        }
        else {
            BubbleIndex.isGUI = true;
            GUI.GUImain(null);
        }
    }
}
